<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/6/23
 * Time: 10:31
 */

namespace app\asmpasync\service;

use app\common\service\IpsConfig;
use app\common\system\SystemConfigService;
use app\common\wipf\WipfConfig;
use app\common\wipf\WipfRuleService;
use Wipf\Config;
use Wipf\Iptables;
use Wipf\Rule;
use Wipf\Webcmd;
use Wipf\WipfRules;

class Wipf
{
    public $classDescription = '函谷盾操作';//模块描述
    public $actionDescription = '';//操作描述

    /**
     * @name 所有函谷盾规则
     * @param $host_id
     * @return WipfRules
     * @author wx
     * @date 2020/6/23 11:27
     */
    function WipfRules($host_id)
    {
        $this->actionDescription = '规则下发';

        //获取ips设置
        $IpsConfig = new IpsConfig();
        $configData = $IpsConfig->findData(['host_id'=> $host_id]);

        //获取防火墙默认处理方式
        $SystemConfigService = new SystemConfigService();
        $defaultProcess = $SystemConfigService->firewallDefaultHandle();

        //获取主机防火墙规则
        $WipfRuleService = new WipfRuleService($host_id, $defaultProcess);
        $rules = $WipfRuleService->allRule($configData);

        $ruleData = $this->rule($rules);
        //设置wipf规则组包
        $Rules = new WipfRules();
        $Rules->setWipfrules($ruleData);

        return $Rules;
    }

    /**
     * @name: 单个规则组装
     * @Param:
     * @Description:
     * @Author: foo
     * @Date: 2019-11-26 15:08:46
     */
    private function rule ($ruleData)
    {
        $return = [];

        foreach ($ruleData as $v)
        {
            $Rule = new Rule();
            $Rule->setRule($v['rule']);
            $Rule->setMode($v['mode']);
            $return[] = $Rule;
        }

        return $return;
    }

    /**
     * @name 下发配置
     * @author wx
     * @date 2020/6/23 10:48
     */
    function Config($host_id)
    {
        $this->actionDescription = '配置下发';

        $SystemConfigService = new SystemConfigService();
        $defaultProcess = $SystemConfigService->firewallDefaultHandle();

        $WipfConfig = new WipfConfig($host_id);
        $config = $WipfConfig->clientConfig($defaultProcess);

        $server = new Config();
        $server->setWipfMode($config['WipfMode']);
        $server->setIpsProcessStatus($config['IpsProcessStatus']);
        $server->setIdsDevice($config['IdsDevice']);
        $server->setIpsDevice($config['IpsDevice']);
        $server->setIpsBlackList($config['IpsBlackList']);
        $server->setLock($config['Lock']);
        $server->setHandle($config['Handle']);

        return $server;
    }

    /**
     * @name 防火墙规则
     * @author wx
     * @date 2020/6/23 13:12
     */
    function Iptables($host_id)
    {
        $this->actionDescription = '防火墙规则下发';
        $ipsData = db('ips_original_rule')->field('rule, status as mode')
            ->where('host_id', $host_id)->select();

        foreach ($ipsData as $k => $v){

            if ($v['mode'] == 1) {
                $ipsData[$k]['mode'] = 'monitor';
            }elseif ($v['mode'] == 0){
                $ipsData[$k]['mode'] = 'unmonitor';
            }else{
                $ipsData[$k]['mode'] = 'delrule';
            }
        }

        $return = $this->rule($ipsData);

        $server = new Iptables();
        $server->setIptables($return);

        return $server;
    }


    /**
     * @name 清空黑名单
     * @param $host_id
     * @author wx
     * @date 2020/6/23 15:10
     */
    function Webcmd($host_id, $action)
    {
        $this->actionDescription = 'web配置下发';
        echo $action;

        $server = new Webcmd();
        $server->setCmd($action);

        return  $server;
    }
}